/*
 * ZENworks Notification Icon
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename ZLauncher.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.novell.zenworks.agent.zicon;

import com.novell.zenworks.agent.common.ICommonUIEnums.MB_ICON;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_RESPONSE;
import com.novell.zenworks.agent.common.ICommonUIEnums.MB_TYPE;
import com.novell.zenworks.agent.zbrowser.MessageBoxLauncher;
import com.novell.zenworks.agent.zicon.remoting.ZUIServer;
import com.novell.zenworks.zicon.common.IZenIconUIServer;
import com.novell.zenworks.zicon.common.LogUtil;

/**
 * Graphics for ZENworks System Tray Icon. Launched as a stand-alone program (java -jar ...)
 *
 */
public class ZLauncher
{
	private static IZenIconUIServer zuiServer=null;
    private static void displayTrayIcon() throws Throwable
    {
        ZTrayIcon trayIcon = ZTrayIcon.getInstance();
        createZUIServer(trayIcon); //RMI for UI remote calls.

        while (!trayIcon.getDisplay().isDisposed())
        {
            if (!trayIcon.getDisplay().readAndDispatch())
            {
                trayIcon.getDisplay().sleep();
            }
        }
        trayIcon.dispose();
    }

    /**
     * main method. This is the ZENworks System Tray Icon launcher.
     * @throws Exception 
     */
    public static void main(String[] args)
    {
        try
        {
            displayTrayIcon();
        }
        catch(Throwable e)
        {
            try
            {
                LogUtil.LogException(e.getMessage(), e);
                
                if(e.getMessage() != null)
                {
                    MessageBoxLauncher.ShowMessageBoxHandler(e.getMessage(), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                }
                else
                {
                    MessageBoxLauncher.ShowMessageBoxHandler(ZTrayIcon.rm.getString("ZiconError"), ZTrayIcon.rm.getString("PropDlgTitle") , MB_TYPE.MBT_OK, MB_ICON.MBI_ERROR, 30, false, MB_RESPONSE.MBR_OK);
                }
                    
            } catch (Exception e1)
            {
                LogUtil.LogException(e.getMessage(), e1);
            }
        }
    }
    
    private static void createZUIServer(ZTrayIcon icon) throws Throwable
    {
    	zuiServer = ZUIServer.createUIServerInstance(icon);
        try
        {
            ZTrayIcon.getInstance().getDisplay().timerExec(0, ZIconServerPing.getInstance());
        	//ZTrayIcon.getServer().setUIServer(RemotingConstants.ZICON_CLIENT_REMOTING_ID, zuiServer);
            //RemotingUtility.registerRemoteObject(RemotingConstants.ZICON_CLIENT_REMOTING_ID, zuiServer); 
        }
        catch(Exception e)
        {
            String message = ZTrayIcon.rm.getString("RemotingError");
            throw new Exception(message);
        }
    }

    public static IZenIconUIServer getZuiServer()
    {
        return zuiServer;
    }

}
